define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    $.widget('rk.colleagueContentMyFollow', {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _init:function(){
            var me = this;
            me.loaddata("myFollowUser",{pageNo:1,pageSize:20});
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            elem.on("click", "[act='colleague_content_myFollow_button']", function(){
                var follow = $(this);
                var uid = follow.attr("uid");
                var followStatus = follow.attr("follow");
                if(follow.hasClass('disable')) return;
                follow.addClass('disable');
                if(followStatus == 1){
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function () {
                        rk.post("/json/oa_relation/delFollow.action", {fId: uid}, function (json) {
                            if (json.status == 0) {
                                follow.attr("follow", "0");
                                follow.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                follow.removeClass("has_follow");
                                follow.addClass("no_follow");
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            follow.removeClass('disable');
                        });
                    },function(){});
                }else if(followStatus == 0){
                    rk.post("/json/oa_relation/addFollow.action",{fId:uid},function(json){
                        if(json.status == 0) {
                            follow.attr("follow", "1");
                            follow.attr("title", rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            follow.removeClass("no_follow");
                            follow.addClass("has_follow");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        follow.removeClass('disable');
                    });
                }
            });
            elem.on("click", "[act='colleague_content_follow_group_button']", function(){
                var button = $(this);
                var isFollow = button.attr("isFollow");
                if(button.hasClass('disable')) return;
                button.addClass('disable');
                var gid = button.attr("gid");
                if(isFollow == "1"){
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function () {
                        rk.post("/json/oa_group/member-delfollow.action", {gid : gid}, function (json) {
                            if (json.status === 0) {
                                button.addClass("no_follow");
                                button.removeClass("has_follow");
                                button.attr("isFollow", "0");
                                button.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else if (json.status === 217002) {
                                rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            button.removeClass('disable');
                        });
                    },function(){});
                }else if(isFollow == "0"){
                    rk.post("/json/oa_group/member-follow.action",{gid : gid},function(json){
                        if(json.status === 0){
                            button.removeClass("no_follow");
                            button.addClass("has_follow");
                            button.attr("isFollow","1");
                            button.attr("title",rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else if(json.status === 217002){
                            rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        button.removeClass('disable');
                    });
                }
            });
            /*elem.on("click", "[act='colleague_content_follow_type']", function(){
                var select = $(this);
                var type = select.val();
                me.loaddata(type,{pageNo:1,pageSize:20});
            });*/
        },
        loaddata:function(type,param){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if(!param){
                param = {pageNo:1,pageSize:20};
            }
            param.lookedUser = opt.uid;
            elem.find("[act='colleague_content_follow_myFollowTable']").beLoading();
            if(type == "myFollowUser") {
                rk.post("/json/oa_relation/following-users.action", param, function (json) {
                    if (json.status == 0) {
                        var temp = require("oa/tmpl/colleague/content/tpl_colleague_content_myFollow.tpl");
                        var followingUsers = json.followingUsers;
                        if (followingUsers && followingUsers.length > 0) {
                            $.each(followingUsers, function (i, dataJson) {
                                if (!dataJson.smallIcon) {
                                    dataJson.smallIcon = rk.DEFAULT_USER_ICON;
                                    followingUsers[i] = dataJson;
                                }
                            });
                        }
                        json.followingUsers = followingUsers;
                        var html = rk.templateText(temp, json);
                        elem.html(html);
                        elem.find('[act="colleague_content_myFollowUser_page"]').rkPaging({
                            pagenum: json.pageNo,  //（可选）当前页数，默认1
                            pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                            rowscount: json.dataCount, //所有数据数
                            onChange: function (pagenum) {
                                var params = param;
                                params.pageNo = pagenum;
                                params.pageSize = json.pageSize;
                                me.loaddata(type, params);
                            }
                        });
                        elem.find("[act='colleague_content_follow_type']").selectmenu({
                            change:function(){
                                var select = elem.find("[act='colleague_content_follow_type']");
                                var type = select.val();
                                $.proxy(me.loaddata(type),me);
                            }
                        });
                    } else {
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                    elem.find("[act='colleague_content_follow_button']").beTip({dir:"left"});
                });
            }else if(type == "myFollowGroup"){
                rk.post("/json/oa_relation/following-groups.action", param, function (json) {
                    if (json.status == 0) {
                        if(json.groups && json.groups.length > 0) {
                            var temp = require("oa/tmpl/colleague/content/tpl_colleague_content_myFollow_group.tpl");
                            var html = rk.templateText(temp, json);
                            elem.find("[act='colleague_content_follow_myFollowTable']").html(html);
                            elem.find('[act="colleague_content_myFollowGroup_page"]').rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loaddata(type, params);
                                }
                            });
                            elem.find("[act='colleague_content_no_follow']").hide();
                        }else{
                            elem.find("[act='colleague_content_no_follow']").show();
                        }
                    } else {
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                    elem.find("[act='colleague_content_follow_button']").beTip({dir:"left"});
                });
            }
        }
    });
});